package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.domain.DictDO;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ConsultationsDO;
import com.jinzhi.jzweb.service.ConsultationsService;
import com.jinzhi.sys.domain.RoleDO;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.RoleConsultationsService;
import com.jinzhi.sys.service.RoleService;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 
 * <pre>
 * 资讯表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/consultations")
public class ConsultationsController extends BaseController {
	@Autowired
	private ConsultationsService consultationsService;
	@Autowired
	private UserService userService;
	@Autowired
	private DictService dictService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleConsultationsService roleConsultationsService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:consultations:consultations")
	String Consultations(){
	    return "jzweb/consultations/consultations";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:consultations:consultations")
	public Result<Page<ConsultationsDO>> list(ConsultationsDO consultationsDTO){
		// 查询列表数据
		Page<ConsultationsDO> page = consultationsService.selectPage(getPage(ConsultationsDO.class),
				consultationsService.convertToEntityWrapper("title", consultationsDTO.getTitle(),"state",consultationsDTO.getState())
						.orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ConsultationsDO obj = (ConsultationsDO) page.getRecords().get(i);
			if (obj.getCreaterId() != null){
				UserDO userDO = userService.selectById(obj.getCreaterId());
				if (userDO != null) {
					obj.setCreaterName(userDO.getUsername());
				}
			}
			if (obj.getCheckerId() != null){
				UserDO userDO = userService.selectById(obj.getCheckerId());
				if (userDO != null) {
					obj.setCheckerName(userDO.getUsername());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:consultations:add")
	String add(Model model){
		Map<String, String> map = new HashMap<>();
		map.put("name", "资讯栏目");
		map.put("type", "information_column");
		List<Map> maps = dictService.selTypeNameVue(map);
		if(maps != null && maps.size() > 0){
			for(int i=0;i<maps.size();i++){
				map.clear();
				map.put("name", maps.get(i).get("name").toString());
				map.put("type", maps.get(i).get("type").toString());
				List<Map> mapsZi = dictService.selTypeNameVue(map);
				maps.get(i).put("second",mapsZi);
			}
			model.addAttribute("section", maps);
		}else{
			model.addAttribute("section", "");
		}
		List<RoleDO> roles = roleService.selectList(null);
        model.addAttribute("roles", roles);
	    return "jzweb/consultations/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:consultations:edit")
	String edit(@PathVariable("id") Long id,Model model){

		ConsultationsDO consultations = consultationsService.selectById(id);
		model.addAttribute("consultations", consultations);
		
		List<RoleDO> roles = roleService.findListStatusByConsultationsId(id);
        model.addAttribute("roles", roles);
	    return "jzweb/consultations/edit";
	}

	@Log("跳转获取栏目信息")
	@ResponseBody
	@GetMapping("/dicLists")
	public Result<?> dicLists(String type) {
		//有数据，则为查二级栏目信息；没数据，则为查一级栏目信息
		if(StringUtils.isBlank(type)){
			type = "information_column";
		}

		List<Map> maps = dictService.selByTypeVue(type);
		return Result.ok(maps);
	}

	@GetMapping("/info/{id}")
	@RequiresPermissions("jzweb:consultations:edit")
	String info(@PathVariable("id") Long id,Model model){
		ConsultationsDO consultations = consultationsService.selectById(id);
		model.addAttribute("consultations", consultations);
		return "jzweb/consultations/info";
	}


	@Log("添加资讯表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:consultations:add")
	public Result<String> save( ConsultationsDO consultations){
		DictDO dictDO = dictService.findOneByKv("type",consultations.getFirstColumn());
		consultations.setFirstColumn(dictDO.getValue());
		DictDO dictDO1 = dictService.findOneByKv("type",consultations.getSecondColumn());
		consultations.setSecondColumn(dictDO1.getValue());

		consultations.setCreaterId(this.getUserId());
		consultationsService.insert(consultations);
        return Result.ok();
	}
	
	@Log("修改资讯表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:consultations:edit")
	public Result<String>  update( ConsultationsDO consultations){
		DictDO dictDO = dictService.findOneByKv("type",consultations.getFirstColumn());
		consultations.setFirstColumn(dictDO.getValue());
		if(StringUtils.isNotBlank(consultations.getSecondColumn())) {
			DictDO dictDO1 = dictService.findOneByKv("type",consultations.getSecondColumn());
			if(dictDO1 != null){
				consultations.setSecondColumn(dictDO1.getValue());
			}
		}

		boolean update = consultationsService.update(consultations);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除资讯表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:consultations:remove")
	public Result<String>  remove( Long id){
		consultationsService.deleteById(id);
		roleConsultationsService.deleteByConsultationsId(id);
        return Result.ok();
	}
	
	@Log("批量删除资讯表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:consultations:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		consultationsService.deleteBatchIds(Arrays.asList(ids));
		roleConsultationsService.deleteListByConsultationsId(ids);
		return Result.ok();
	}
	
	@Log("审核")
	@PostMapping( "/review")
	@ResponseBody
	@RequiresPermissions("jzweb:consultations:review")
	public Result<String>  review(@RequestParam("ids[]") Long[] ids,Integer state,String remark){
		if (state.equals(2) || state.equals(1)) {
			int i = 0;
			for (Long id : ids) {
				ConsultationsDO consultationsDO = consultationsService.selectById(id);
				if(consultationsDO != null){
					consultationsDO.setState(state);
					consultationsDO.setCheckerId(this.getUserId());
					consultationsDO.setCheckerDate(new Date());
					if(state.equals(2)){
						consultationsDO.setRemark(remark);//未通过，添加备注
					}else{
						consultationsDO.setRemark("");//审核通过，清空备注
					}
					boolean b = consultationsService.updateById(consultationsDO);
					if (b) {
						i++;
					} else {
						break;
					}
				}
			}
			if (i == ids.length) {
				return Result.build(0,"已成功审核：" + i + "条数据");
			} else {
				return Result.fail("审核状态修改失败！");
			}
		} else {
			return Result.fail("审核状态修改失败！");
		}
	}
}
